package handler

import (
	"github.com/gin-gonic/gin"
	"tie/api/register"
	"tie/initlist/mysql"
	"tie/inits/model"
)

func Follow(r *gin.Context) {
	var req register.Follow
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数失败",
			Data:    nil,
		})
		return
	}

	tr := mysql.DB.Begin()

	m := model.Follow{
		UserId:      req.UserId,
		FollowCount: req.FollowCount,
		FenCount:    req.FenCount,
	}
	err = mysql.DB.Debug().Create(&m).Error
	if err != nil {
		tr.Rollback()
		r.JSON(400, &Res{
			Code:    400,
			Message: "关注失败",
			Data:    nil,
		})
		return
	}
	tr.Commit()
	r.JSON(200, &Res{
		Code:    200,
		Message: "关注成功",
		Data:    m,
	})
}

func FollowDel(r *gin.Context) {
	var req register.Follow
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数失败",
			Data:    nil,
		})
		return
	}

	tr := mysql.DB.Begin()

	var follow model.Follow
	err = mysql.DB.Debug().Where("id = ?", follow.ID).Delete(&model.Follow{}).Error
	if err != nil {
		tr.Rollback()
		r.JSON(400, &Res{
			Code:    400,
			Message: "取消失败",
			Data:    nil,
		})
		return
	}
	tr.Commit()
	r.JSON(200, &Res{
		Code:    200,
		Message: "取消成功",
		Data:    follow.ID,
	})
}
